package com.huazhi.wzsnw.util.redis;


import com.huazhi.wzsnw.mapper.DdlinedMapper;
import com.huazhi.wzsnw.mapper.LinedMapper;
import com.huazhi.wzsnw.model.Ddlined;
import com.huazhi.wzsnw.model.Lined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;


/**
 * @author zhengch
 * @date 2022年05月23日 2:35 下午
 */


public class KeyExpiredListener extends KeyExpirationEventMessageListener {
    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 使用该方法监听，当我们key过期是自动调用该方法
     *
     * @param message
     * @param pattern
     */

    @Autowired
    LinedMapper linedMapper;
    @Autowired
    DdlinedMapper ddlinedMapper;
    @Autowired
    RedisCache redisCache;

    private final static String PREFIX_ID_NUM = "SHORT_ID_NUM:";
    private final static String PREFIX_DKLJ_NUM = "DKLJ_NUM:";
    private final static String PREFIX_NBYC_NUM = "NBTC_NUM:";
    private final static String PREFIX_HEART_NUM = "HEART_NUM:";
    private final static String PREFIX_HEART_DATA_NUM = "HEART_DATA_NUM:";
    private final static String PREFIX_SEND_DATA_NUM = "SEND_DATA_NUM:";

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key = message.toString();//就是过期的key
        System.out.println("redis的key" + key + "已经过期！");
        //首先判断是编号过期还是数据过期
        if (key.contains("HEART_NUM")) {
            //断开链接的情况
            dklj(key.replace(PREFIX_HEART_NUM, ""));
        } else {
            //有心跳没有数据的情况
            nbyc(key.replace(PREFIX_HEART_DATA_NUM, ""));
        }

    }

    public void dklj(String num) {
        if (num.contains("WZSNWV1")) {
            //中海龙
            Lined lined = linedMapper.getByDtu(num);
            if (lined != null) {
                lined.setState("2");
                linedMapper.updateByPrimaryKey(lined);
                redisCache.setCacheObject(PREFIX_DKLJ_NUM + num, num);
                System.out.println("编号为"+num+"断开链接");
            } else {
                System.out.println("redis断开链接的key为" + num + "过期，但是没有找到相关编号的设备");

            }
        } else {
            //东大
            Ddlined ddlined = ddlinedMapper.selectByDtuNum(num);
            if (ddlined != null) {
                ddlined.setState("2");
                ddlinedMapper.updateByPrimaryKey(ddlined);
                redisCache.setCacheObject(PREFIX_DKLJ_NUM + num, num);
                System.out.println("编号为"+num+"断开链接");
            } else {
                System.out.println("redis断开链接的key为" + num + "过期，但是没有找到相关编号的设备");
            }

        }
    }

    public void nbyc(String num) {
        if (num.contains("WZSNWV1")) {
            //钟海龙
            Lined lined = linedMapper.getByDtu(num);
            if (lined != null) {
            if(!"2".equals(lined.getState())){
                //如果现在的状态不是断开链接
                lined.setState("3");
                linedMapper.updateByPrimaryKey(lined);
                redisCache.setCacheObject(PREFIX_NBYC_NUM+num,num);
                System.out.println("编号为"+num+"内部异常");
            }
            } else {
                System.out.println("redis内部异常的key为" + num + "过期，但是没有找到相关编号的设备");
            }

        } else {
            //东大
            Ddlined ddlined = ddlinedMapper.selectByDtuNum(num);
             if(ddlined!=null){
                 if(!"2".equals(ddlined.getState())){
                   ddlined.setState("3");
                   ddlinedMapper.updateByPrimaryKey(ddlined);
                   redisCache.setCacheObject(PREFIX_ID_NUM+num,num);
                     System.out.println("编号为"+num+"内部异常");
                 }
             }else {
                 System.out.println("redis内部异常的key为" + num + "过期，但是没有找到相关编号的设备");
             }
        }
    }


}
